package com.bosssoft.hr.train.jsp.example.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;


public class JSONUtil {
    private JSONUtil(){}
    public static JSONObject getJson(HttpServletRequest request) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;

        BufferedReader bufferedReader = request.getReader();
        while((line = bufferedReader.readLine())!=null){
            stringBuilder.append(line);
        }

        String json = stringBuilder.toString();
        return JSON.parseObject(json);

    }

    public static void writeJson(HttpServletResponse resp, JSONObject resultJson) throws IOException{
        PrintWriter printWriter = resp.getWriter();
        printWriter.write(resultJson.toJSONString());
        printWriter.flush();
        printWriter.close();
    }

}
